$NG.AllReady(function (page, { useDataIndexChange, useUpdateRow,useValuesChange ,useBeforeClick, useUpdateRows,useClick }) {
    // $NG.updateState((updater)=>{
    //     updater.data.p_form_facility_visit.setProps({
    //         phid_pc:{
    //             label:"20241008测试(20241008-0001)",
    //             value:"8570000000000002"
    //         },
    //         u_visot_time:'2000-02-01'
    //     })
    // })
    // useBeforeClick((prop)=>{
    //     console.log(prop);
    //     const staTime = 3*1000+new Date().getTime()
    //     while(true){
    //         const datei = new Date().getTime()
    //         if(datei>staTime){
    //             break
    //         }
    //     }
    //     return true
    // },'save')
    // useClick((prop)=>{
    //     console.log(prop,'ccc');
    // },'save')
    console.log('双击成功!!!!!', window.location.href);
    const mstform = $NG.getCmpApi("p_form_facility_visit")
    const _paramURL = parseUrlParams(window.location.href)
    console.log(_paramURL);
    // mstform.setHidden("u_score_id")
    // mstform.setHidden("u_if_citation")
    //【注意这里的表名不能为控件ID，必须要是表名】
    $NG.updateUI((updater)=>{
        updater.fieldSetForm.p_form_facility_visit.u_score_id.setProps({
            hidden: true,   //隐藏
        });
        updater.fieldSetForm.p_form_facility_visit.u_if_citation.setProps({
            hidden: true,   //隐藏
        });
    })
        if(_paramURL.inType == "click"){
            console.log(`点击进入`);
            if(page.oType=='add'){
                $NG.updateState((updater)=>{
                    console.log(`增加`,_paramURL.phidName.replaceAll(" ","")||"",_paramURL.phidPc.replaceAll(" ","")||"");
                    updater.data.p_form_facility_visit.setProps({
                        u_visit_psn:{
                            label:_paramURL.u_gxr_name.replaceAll(" ","") || "",
                            value:_paramURL.u_gxr.replaceAll(" ","") || ""
                        },
                        phid_pc:{
                            label:"20241008测试(20241008-0001)",
                            value:"8570000000000002"
                        },
                        u_v_plan_visit:{
                            label:_paramURL.bookName.replaceAll(" ",""),
                            value:_paramURL.bookId.replaceAll(" ","")
                        },
                        u_visot_time:formatDate(new Date(), "yyyy-mm-dd"),
                        u_score_id:_paramURL.sourceId.replaceAll(" ","")
                    })
                })
            }
            console.log(mstform.getItem("u_score_id").getValue());
        }

        if(page.oType=='add'){
            //设置回访时间
            $NG.updateState((updater)=>{
                updater.data.p_form_facility_visit.setProps({
                    u_visot_time:formatDate(new Date(),'yyyy-mm-dd')
                })
            })
        }
    useValuesChange(({args})=>{
        console.log(`监听项目`, args[0])
    }, "p_form_facility_visit.phid_pc")

    useValuesChange(({args})=>{
        console.log(`监听回访对象`, args[0])
        $NG.updateState((updater)=>{
            updater.data.p_form_facility_visit.setProps({
                u_v_plan_visit:{
                    label:null,
                    value:null
                }
            })
        })
    }, "p_form_facility_visit.u_visit_psn")
    useValuesChange(({args})=>{
        getVisitInfo()
    },"p_form_facility_visit.u_v_plan_visit")
    //干系人过滤，放弃
function gxrMFilter() {
    $NG.updateUI((updater, state) => {
        console.log('过滤参数检查', updater,$NG.getCmpApi('p_form_facility_visit').getItem('phid_pc').getValue());
        updater.form.p_form_facility_visit.u_visit_psn.setProps({
            onBeforeOpen: async () => {
                var phid_pc = $NG.getCmpApi('p_form_facility_visit').getItem('phid_pc').getValue();
                if (!phid_pc) {
                    $NG.alert("请先选择项目");
return false;   
                } else {
                    return true;
                }
            },
            ...(!!$NG.getCmpApi('p_form_facility_visit').getItem('phid_pc').getValue() && {
                clientSqlFilter: {
                    'm.phid_pc': $NG.getCmpApi('p_form_facility_visit').getItem('phid_pc').getValue()
                }
            }),
        });
    });
}

//获取现场计划单
function getVisitInfo(){
    $NG.execServer("GetVisitInfo",{v_plan_visit:mstform.getItem("u_v_plan_visit").getValue()},(res)=>{
        console.log(`计划单信息`,res,mstform.getItem("u_v_plan_visit").getValue());
        if(res.count==0)return
        const _data = JSON.parse(res.data)[0].extendObjects
        $NG.updateState((updater)=>{
            updater.data.p_form_facility_visit.setProps({
                phid_pc:{
                    label:_data.pc_name,
                    value:_data.pc_phid
                },
                u_visit_psn:{
                    label:_data.u_gxr_xm,
                    value:_data.u_plan_visit_ps
                }
            })
        })
    })
}
    // 解析参数
    function parseUrlParams(url) {
        const paramsObj = {};
        const queryString = url.split('?')[1];
        const paramPairs = queryString.split('&');
        paramPairs.forEach((paramPair) => {
            const [key, value] = paramPair.split('=');
            const decodedValue = decodeURIComponent(value);
            paramsObj[key] = decodedValue;
        });
        return paramsObj;
    }

 // 格式化日期
function formatDate(dateStr, outputFormat) {
    const date = new Date(dateStr);

    if (isNaN(date)) {
        return "无效的日期字符串";
    }

    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');

    let formattedDate;

    switch (outputFormat) {
        case "yyyy-mm-dd":
            formattedDate = `${year}-${month}-${day}`;
            break;
        case "yyyy-mm-dd HH:mm":
            formattedDate = `${year}-${month}-${day} ${hours}:${minutes}`;
            break;
        case "yyyy-mm":
            formattedDate = `${year}-${month}`;
        break;
        // 可以根据需要继续添加其他格式的处理逻辑
        default:
            formattedDate = "不支持的输出格式";
    }

    return formattedDate;
}
//过滤
function jhMFilter() {
    $NG.updateUI((updater, state) => {
        console.log('过滤参数检查', updater);
        updater.form.p_form_facility_visit.u_v_plan_visit.setProps({
            onBeforeOpen: async () => {
                var phid_pc = mstform.getItem('u_visit_psn').getValue();
                if (!phid_pc) {
                    $NG.alert("请先选择回访对象");
                    return false;
                } else {
                    return true;
                }
            },
            ...(!!mstform.getItem('u_visit_psn').getValue() && {
                clientSqlFilter: {
                    u_plan_visit_ps:mstform.getItem("u_visit_psn").getValue()
                }
            }),
        });
    });
}
});

